package sliver.action.shop;

import java.io.Writer;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.entity.Order;
import sliver.service.shop.order.OrderCRUDService;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("shop.queryOrderItemsAction")
public class QueryOrderItemsAction extends ActionSupport{

	private static final long serialVersionUID = 8783321084855816490L;
	
	private Long orderId;
	
	@Autowired
	private OrderCRUDService orderCRUDService;
	
	public QueryOrderItemsAction(){
		
	}

	public Long getOrderId() {
		return orderId;
	}

	public void setOrderId(Long orderId) {
		this.orderId = orderId;
	}
	
	@Override
	public String execute() throws Exception {
		
		Order order = orderCRUDService.queryTargetOrder(orderId);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		
		Writer writer = response.getWriter();
		if(order != null)
			writer.write(JSON.toJSONString(order));
		else
			writer.write("{}");
		
		writer.flush();
		
		return null;
	}
	
}
